package com.tuotu.gps.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewStub;
import android.widget.TextView;

import com.tuotu.gps.R;
import com.tuotu.gps.adapter.MyRecycleViewAdapter;
import com.tuotu.gps.entity.LocationInfo;
import com.tuotu.gps.utils.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;

/**
 * Created by XuYeWen on 2018/3/13.
 */

public class ItemActionDialog extends Dialog {

    private ViewStub mLayoutActionDo;
    private ViewStub mLayoutActionDelete;
    private ViewStub mLayoutActionCover;

    private View mLayoutActionDoView;
    private View mLayoutActionDeleteView;
    private View mLayoutActionCoverView;
    private itemDialogListener mItemDialogListener;

    TextView tvNameMp3;
    TextView tvNameTxt;
    TextView tvDirection;
    TextView tvGpsInfo;
    TextView tvDuration;
    TextView tvDuration0;
    TextView tvNumber;
    private Context mContext;
    private int mPosition;
    private LocationInfo mLocationInfo;

    public ItemActionDialog(@NonNull Context context, int selectedPosition, LocationInfo locationInfo) {
        super(context, R.style.SelfDialog);
        this.mContext=context;
        this.mPosition=selectedPosition;
        this.mLocationInfo=locationInfo;
    }

    public void setmItemDialogListener(itemDialogListener mItemDialogListener) {
        this.mItemDialogListener = mItemDialogListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_action_layout);
        initGpsView();
        initActionViewDo();
        initActionViewDelete();
        initActionViewCover();
        setViewVisibility(0);
    }

    private void initGpsView() {
        tvNumber = (TextView) findViewById(R.id.tv_number);
        tvNameMp3 = (TextView) findViewById(R.id.tv_name_mp3);
        tvNameTxt = (TextView) findViewById(R.id.tv_name_txt);
        tvDirection = (TextView)findViewById(R.id.tv_direction);
        tvGpsInfo= (TextView)findViewById(R.id.tv_name_gpsinfo);
        tvDuration = (TextView) findViewById(R.id.tv_duration);
        tvDuration0 = (TextView) findViewById(R.id.tv_duration0);

        setItemLocationInfo(mPosition,mLocationInfo);
    }

    private void initActionViewCover() {
        mLayoutActionCover = findViewById(R.id.layout_action_cover);
        mLayoutActionCoverView = mLayoutActionCover.inflate();
        mLayoutActionCoverView.setVisibility(View.GONE);
        mLayoutActionCoverView.findViewById(R.id.item_action_cover_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != mItemDialogListener)
                    mItemDialogListener.itemVoiceCoverConfirm();
                dismiss();
            }
        });
        mLayoutActionCoverView.findViewById(R.id.item_action_cover_cancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setViewVisibility(0);
            }
        });
    }

    private void initActionViewDelete() {
        mLayoutActionDelete = findViewById(R.id.layout_action_delete);
        mLayoutActionDeleteView = mLayoutActionDelete.inflate();
        mLayoutActionDeleteView.setVisibility(View.GONE);
        mLayoutActionDeleteView.findViewById(R.id.item_action_delete_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != mItemDialogListener)
                    mItemDialogListener.itemVoiceDeleteConfirm();
                dismiss();
            }
        });
        mLayoutActionDeleteView.findViewById(R.id.item_action_delete_cancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setViewVisibility(0);
            }
        });
    }

    private void initActionViewDo() {
        mLayoutActionDo = findViewById(R.id.layout_action_do);
        mLayoutActionDoView = mLayoutActionDo.inflate();
        mLayoutActionDoView.findViewById(R.id.item_action_do_play).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != mItemDialogListener)
                    mItemDialogListener.itemVoicePlay();
                dismiss();
            }
        });
        mLayoutActionDoView.findViewById(R.id.item_action_do_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setViewVisibility(1);
            }
        });
        mLayoutActionDoView.findViewById(R.id.item_action_do_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != mItemDialogListener)
                    mItemDialogListener.itemVoiceAdd();
                dismiss();
            }
        });
        mLayoutActionDoView.findViewById(R.id.item_action_do_cover).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setViewVisibility(2);
            }
        });
        mLayoutActionDoView.findViewById(R.id.item_action_do_cancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cancel();
            }
        });
    }

    private void setViewVisibility(int i){
        mLayoutActionDo.setVisibility(i==0?View.VISIBLE:View.GONE);
        mLayoutActionDelete.setVisibility(i==1?View.VISIBLE:View.GONE);
        mLayoutActionCover.setVisibility(i==2?View.VISIBLE:View.GONE);
    }

    public void setItemLocationInfo(int position, LocationInfo locationInfo) {
        tvNumber.setText(" "+(position+1));
        String name = locationInfo.getMp3Name();
        String str = name.substring(11, name.length() - 4).replace("_", ":");
        tvNameMp3.setText(str);
        Date date=new Date();
//        Log.i("xyw","read file name=="+GPS_PATH+name+".txt");

        String gpsinfo = Utils.readFile(locationInfo.getTxtPathName());
        if (null != gpsinfo) {
//            Log.i("xwww","gpsinfo =="+gpsinfo);
            int duration = 0;
            try {
                JSONObject jsonObject = new JSONObject(gpsinfo);
                double lon=jsonObject.getDouble("lon");
                double lat=jsonObject.getDouble("lat");
                tvGpsInfo.setText(" "+(float)lon+" "+(float)lat); //显示gps经纬度
                tvDirection.setText(Utils.getDirection(jsonObject.getDouble("direction")));
//                long t1=preferences.getLong(list.get(position).getMp3Name(), 0);
                long t1 = jsonObject.getLong("collect_time");
                if (0 != t1) {
                    Date pDate=new Date(t1);
                    String pMp3String=Utils.itemTimeFormat.format(pDate);
                    tvNameMp3.setText(pMp3String);

                    tvNameTxt.setVisibility(View.VISIBLE);
                    long t2 = date.getTime() - t1;
                    int day = (int) (t2 / 24 / 60 / 60 / 1000);
                    int hour = (int) (t2 / 60 / 60 / 1000);
                    int fen = (int) (t2 / 60 / 1000);
                    int miao = (int) (t2 / 1000);
                    if (day > 0) {
                        tvNameTxt.setText(day + "天");
                    } else if (hour > 0) {
                        tvNameTxt.setText(hour + "小时");
                    } else if (fen > 0) {
                        tvNameTxt.setText(fen + "分钟前");
                    } else {
                        tvNameTxt.setText(miao + "秒钟前");
                    }
                } else {
                   tvNameTxt.setVisibility(View.GONE);
                }

                duration = jsonObject.getInt("mp3_duration");
//                LogUtil.printE("duration=="+duration);
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (Exception e) {

            }

            tvDuration0.setVisibility(duration == 0 ? View.INVISIBLE : View.VISIBLE);
            tvDuration.setVisibility(duration == 0 ? View.INVISIBLE : View.VISIBLE);
            if (duration != 0){
                tvDuration.setText(duration + "秒");
                if(duration>4){
                    tvDuration0.setTextColor(Color.WHITE);
                    tvDuration.setTextColor(Color.WHITE);
                }else {
                    tvDuration0.setTextColor(ContextCompat.getColor(mContext,R.color.colorOrange));
                    tvDuration.setTextColor(ContextCompat.getColor(mContext,R.color.colorOrange));
                }
            }
        }
    }

    public interface itemDialogListener {
        void itemVoicePlay();

        void itemVoiceAdd();

        void itemVoiceDeleteConfirm();

        void itemVoiceCoverConfirm();
    }

}
